package cn.qfei.qiyuesuo.service.feishu.impl;

import cn.qfei.qiyuesuo.bo.ApprovalExampleBo;
import cn.qfei.qiyuesuo.bo.ClientModel;
import cn.qfei.qiyuesuo.service.feishu.FeishuContractService;
import cn.qfei.qiyuesuo.service.feishu.FeishuImService;
import cn.qfei.qiyuesuo.service.feishu.util.FeishuClientUtils;
import cn.qfei.qiyuesuo.vo.FeishuContractPageVo;
import cn.qfei.qiyuesuo.vo.FeishuContractVo;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.google.common.base.Joiner;
import com.lark.oapi.Client;
import com.lark.oapi.core.request.RequestOptions;
import com.lark.oapi.core.response.RawResponse;
import com.lark.oapi.core.token.AccessTokenType;
import com.lark.oapi.core.utils.Jsons;
import com.lark.oapi.service.im.v1.model.CreateMessageReq;
import com.lark.oapi.service.im.v1.model.CreateMessageReqBody;
import com.lark.oapi.service.im.v1.model.CreateMessageResp;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author chenlp
 * @Date 2023/8/7 10:17
 **/

@Slf4j
@Service
public class FeishuContractServiceImpl implements FeishuContractService {
    @Autowired
    private FeishuImService feishuImService;

    @Value("${feishu.basePath}")
    private  String fsBasePath;
    @Override
    public Map<String, Object> selectContractById(String contractId, String appId, String appSecret, String tenantAccessToken,String appKey,String appTicket) {
            return feishuImService.selectContractById( contractId,  appId,  appSecret);
    }

    @Override
    public Map<String, Object> getFsProcessInstances(String fsAppId, String fsAppSecret, String processInstanceId) {
        return feishuImService.getFsProcessInstances( processInstanceId,  fsAppId,  fsAppSecret);

    }
    /**
     * 发送仅特定人可见的消息卡片
     */
    @Override
    public int sendCardMessageToPeople(String fsAppId, String fsAppSecret, String receiveId, String msgType, String content,String receiveType) {
        return feishuImService.sendCardMessageToPeople(  fsAppId,  fsAppSecret,  receiveId,  msgType,  content,receiveType);

    }

    @Override
    public String uploadContractFile(String contractId, String appId, String appSecret, String fileType, File file, String fileName) {
        return feishuImService.uploadContractFile(  contractId, appId, appSecret, fileType,file,fileName);
    }

    @Override
    public int updateContract(String contractId, String appId, String appSecret, List<String> attachmentIds) {
        return feishuImService.updateContract(  contractId, appId, appSecret,attachmentIds);

    }

    @Override
    public int updateInstanceAssignee(String processInstanceId, String fsAppId, String fsAppSecret, String taskInstanceId, String assigneeIds) {
        return feishuImService.updateInstanceAssignee(processInstanceId,  fsAppId,  fsAppSecret,  taskInstanceId,  assigneeIds);
    }

    @Override
    public Map<String, Object> selectInstanceById(String instanceId, String fsAppId, String fsAppSecret) {
        return feishuImService.selectInstanceById(instanceId,  fsAppId,  fsAppSecret);

    }

    @Override
    public int tasksApprov(String approvalCode, String appId, String appSecret, String instanceCode, String fsFileId, String openId, String taskId, String form) {
        return feishuImService.tasksApprov(approvalCode, instanceCode, openId, taskId, fsFileId,appId,  appSecret,form);
    }

    @Override
    public File downloadFile( String appId, String appSecret,String fileId, String fileName) {
        byte[] fileByte= feishuImService.downloadFile(fileId,appId,  appSecret);

        return readToFile(fileByte,fileName);
    }

    @Override
    public String uploadApprovalFile(String contractId, String appId, String appSecret, String fileType, File file, String fileName) {
        return feishuImService.uploadApprovalFile(  contractId, appId, appSecret, fileType,file,fileName);

    }

    /**
     * 文件写本地
     * @param fileByte
     * @param fileName
     * @return
     */
    private File readToFile(byte[] fileByte, String fileName) {
        File file=new File(fsBasePath+fileName);

        Boolean flag = false;
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (Exception e) {
                log.error("出错",e);
            }
        }
        file.setExecutable(true);
        file.setReadable(true);
        file.setWritable(true);
        OutputStream os = null;
        try {
            os = new FileOutputStream(file);
//            os.write(context.getBytes("GBK"));
            os.write(fileByte);
            if (os != null) {
                os.close();
            }
            flag = true;
        } catch (Exception e) {
            log.error("出错",e);
            if (os != null) {
                try {
                    os.close();
                } catch (IOException ioException) {
                    log.error("出错",e);
                }
            }
        }
        return file;
    }





}






